package com.zkadmin.controller;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Button;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.zkadmin.entity.SysSetting;
import com.zkadmin.helper.ZkAdminInit;
import com.zkadmin.service.SysSettingService;

@SuppressWarnings("serial")
public class SettingCtrl extends BaseCtrl<Window> {
	@WireVariable
	private SysSettingService sysSettingService;

	@Wire
	private Button saveSettingBtn;

	@Wire
	private Textbox name;

	@Wire
	private Listbox rememberMe;

	@Wire
	private Spinner loginRetryLimit;

	@SuppressWarnings("unused")
	private Window win;

	@Override
	protected void init(Window win) {
		this.win = win;
		SysSetting setting = sysSettingService.getSysSetting();
		name.setValue(setting.getName());
		rememberMe.setSelectedIndex(setting.getRememberMe() ? 0 : 1);
		loginRetryLimit.setValue(setting.getLoginRetryLimit());
	}

	@Listen("onClick=#saveSettingBtn")
	public void saveSetting(Event event) {
		SysSetting setting = sysSettingService.getSysSetting();
		setting.setName(name.getValue());
		setting.setRememberMe(rememberMe.getSelectedIndex() == 0);
		setting.setLoginRetryLimit(loginRetryLimit.getValue());
		sysSettingService.save(setting);
		ZkAdminInit.setSetting(setting);
		Clients.showNotification("系统配置已成功保存！", "info", saveSettingBtn, "end_center", 2000);
	}
}
